package com.hd.tiyu.yuejian.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hd.tiyu.yuejian.JavaBean.Register;
import com.hd.tiyu.yuejian.LoginActivity;
import com.hd.tiyu.yuejian.Model.UserModel;
import com.hd.tiyu.yuejian.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {
    private EditText phone;
    private EditText pass;
    private  Button register_btn;
    private  Button backlogin_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        initViews();
        initEvents();
        initData();
    }

    private void initViews() {
               phone=(EditText) findViewById(R.id.edit_phone);
            pass=(EditText) findViewById(R.id.edit_password);
           register_btn=(Button) findViewById(R.id.button_register);
       backlogin_btn=(Button) findViewById(R.id.bt_back_to_login);
    }

    private void initEvents() {

        register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String tel=phone.getText().toString();
                final String password=pass.getText().toString();
                UserModel usermodel=new UserModel();
                Call<Register> call=usermodel.Register(tel,password);
                call.enqueue(new Callback<Register>() {
                    @Override
                    public void onResponse(Call<Register> call, Response<Register> response) {
                        Register register=response.body();
                        if(!TextUtils.isEmpty(tel)&&!TextUtils.isEmpty(password)){
                            Toast.makeText(RegisterActivity.this, "注册成功"+register, Toast.LENGTH_LONG).show();
                            Intent intent=new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Register> call, Throwable t) {

                    }
                });
            }
        });
        backlogin_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();

            }
        });

    }

    private void initData() {

    }
}
